package com.example.wly.myapplication;

import android.graphics.Color;
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.ProgressBar;
import android.widget.SeekBar;

public class ColorCombinActivity extends AppCompatActivity {
    int red = 0;//颜色分量，初始值
    int green = 0;
    int blue = 0;
    int Alpha = 255;
    private EditText etgreen;
    private EditText etblue;
    private ImageView imcolor;
    private EditText etred;
    private Button btnDel;
    private Button btnAdd;
    private ProgressBar pbRed;
    private Button btnAdd1;
    private Button btnDel1;
    private ProgressBar pbBlue;
    private ProgressBar pbGreen;
    private Button btnAdd2;
    private Button btnDel2;
    private EditText etAlpha;


    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_color_combin);
        imcolor = (ImageView) findViewById(R.id.img);
        etred = (EditText) findViewById(R.id.red);
        etgreen = (EditText) findViewById(R.id.greed);
        etblue = (EditText) findViewById(R.id.blue);
        btnDel = (Button) findViewById(R.id.btn_delete);
        btnAdd = (Button) findViewById(R.id.btn_add);
        pbRed = (ProgressBar) findViewById(R.id.pb_red);
        btnAdd1 = (Button)findViewById(R.id.btn_add1);
        btnDel1 = (Button) findViewById(R.id.btn_delete1);
        pbGreen = (ProgressBar)findViewById(R.id.pb_green);
        btnAdd2 = (Button)findViewById(R.id.btn_add2);
        btnDel2 = (Button)findViewById(R.id.btn_delete2);
        pbBlue = (ProgressBar)findViewById(R.id.pb_blue);
        etAlpha = (EditText) findViewById(R.id.Alpha);
//        SeekBar sRed = (SeekBar) findViewById(R.id.sbar_red);//拖动条控件
//        SeekBar sGreen = (SeekBar) findViewById(R.id.sbar_green);
//        SeekBar sBlue = (SeekBar) findViewById(R.id.sbar_blue);
        SeekBar sAlpha = (SeekBar) findViewById(R.id.sbar_alpha);

//        sRed.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
//            @Override
//            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//                //当进度值发生改变的时候，获取改变之后的进度，作为red颜色分量的值，合成颜色
//                Alpha = progress;//改变之后的进度值赋值给red
//                imcolor.setBackgroundColor(Color.rgb(red,green,blue));
//                Color.rgb(red,blue,green);
//            }
//
//            @Override
//            public void onStartTrackingTouch(SeekBar seekBar) {
//
//            }
//
//            @Override
//            public void onStopTrackingTouch(SeekBar seekBar) {
//
//            }
//        });

//        sGreen.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
//            @Override
//            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//                green = progress;
//                imcolor.setBackgroundColor(Color.rgb(red,green,blue));
//                Color.rgb(red,blue,green);
//
//            }
//
//            @Override
//            public void onStartTrackingTouch(SeekBar seekBar) {
//
//            }
//
//            @Override
//            public void onStopTrackingTouch(SeekBar seekBar) {
//
//            }
//        });
//
//        sBlue.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
//            @Override
//            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
//                blue = progress;
//                imcolor.setBackgroundColor(Color.rgb(red,green,blue));
//                Color.rgb(red,blue,green);
//            }
//
//            @Override
//            public void onStartTrackingTouch(SeekBar seekBar) {
//
//            }
//
//            @Override
//            public void onStopTrackingTouch(SeekBar seekBar) {
//
//            }
//        });

        sAlpha.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
            @Override
            public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
                //当进度值发生改变的时候，获取改变之后的进度，作为red颜色分量的值，合成颜色
                Alpha = progress;//改变之后的进度值赋值给red
                imcolor.setBackgroundColor(Color.argb(Alpha,red,green,blue));
            }

            @Override
            public void onStartTrackingTouch(SeekBar seekBar) {

            }

            @Override
            public void onStopTrackingTouch(SeekBar seekBar) {

            }
        });


        btnAdd2.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v)
            {
                if(pbBlue.getProgress()>245)
                {
                    pbBlue.setProgress(255);
                }
                else
                {
                    pbBlue.setProgress(pbBlue.getProgress()+10);
                }
                blue = pbBlue.getProgress();

                imcolor.setBackgroundColor(Color.rgb(red,green,blue));
            }

        });

        btnDel2.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v)
            {
                if (pbBlue.getProgress()<10)
                {
                    pbBlue.setProgress(0);
                }
                else
                {
                    pbBlue.setProgress(pbBlue.getProgress()-10);
                }
                blue = pbBlue.getProgress();
                imcolor.setBackgroundColor(Color.rgb(red,green,blue));
            }
        });


        btnAdd1.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v)
            {
                if (pbGreen.getProgress()>245)
                {
                    pbGreen.setProgress(255);
                }
                else
                {
                    pbGreen.setProgress(pbGreen.getProgress()+10);
                }
                green = pbGreen.getProgress();
                imcolor.setBackgroundColor(Color.rgb(red,green,blue));
            }
        });

        btnDel1.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v)
            {
                if (pbGreen.getProgress()<10)
                {
                    pbGreen.setProgress(0);
                }
                else
                {
                    pbGreen.setProgress(pbGreen.getProgress()-10);
                }
                green = pbGreen.getProgress();
                imcolor.setBackgroundColor(Color.rgb(red,green,blue));
            }
        });


        btnAdd.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v)
            {
                if (pbRed.getProgress()>245)
                {
                    pbRed.setProgress(255);
                }
                else
                {
                    //pbRed.getProgress();//获取进度条的值

                    pbRed.setProgress(pbRed.getProgress()+10);//设置进度条的进度值
                }

                //红颜色分量值增加
                red = pbRed.getProgress();
                //颜色发生变化
                imcolor.setBackgroundColor(Color.rgb(red,green,blue));
            }
        });

        btnDel.setOnClickListener(new View.OnClickListener(){
            public void onClick(View v)
            {
                if (pbRed.getProgress()<10)
                {
                    pbRed.setProgress(0);
                }
                else
                {
                    pbRed.setProgress(pbRed.getProgress()-10);
                }
                red = pbRed.getProgress();
                imcolor.setBackgroundColor(Color.rgb(red,green,blue));
            }
        });

        //当文本输入框输入完数据后，获取输入框的数据
        //监听输入框文本数据是否发生改变
        etAlpha.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                //改变完之后要做的处理代码
                //文本框输入完之后，获取文本框的输入值
                String AlphaString = etAlpha.getText().toString();
                //判断是否为空串
                if(AlphaString.equals(""))
                {
                    Alpha = 0;
                }
                else
                {
                    Alpha = Integer.parseInt(AlphaString);//将字符串转换成整型数
                }
                //判断数的范围<255
                if(Alpha > 255)
                {
                    Alpha = 255;//最大取值255
                }else if(Alpha < 0){
                    Alpha = 0;//最小取值0
                }
                imcolor.setBackgroundColor(Color.argb(Alpha,red,blue,green));

            }
        });

        etred.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {
                //改变前要做的处理代码

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                //改变时要做的处理代码

            }

            @Override
            public void afterTextChanged(Editable s) {
                //改变完之后要做的处理代码
                //文本框输入完之后，获取文本框的输入值
                String redString = etred.getText().toString();
                //判断是否为空串
                if(redString.equals(""))
                {
                    red = 0;
                }
                else
                {
                    red = Integer.parseInt(redString);//将字符串转换成整型数
                }
                //判断数的范围<255
                if(red > 255)
                {
                    red = 255;//最大取值255
                }else if(red < 0){
                    red = 0;//最小取值0
                }
                imcolor.setBackgroundColor(Color.rgb(red,green,blue));


            }
        });

        etblue.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {

            }

            @Override
            public void afterTextChanged(Editable s) {
                String blueString = etblue.getText().toString();
                if(blueString.equals(""))
                {
                    blue = 0;
                }
                else
                {
                    blue = Integer.parseInt(blueString);//将字符串转换成整型数
                }
                //判断数的范围<255
                if(blue > 255)
                {
                    blue = 255;//最大取值255
                }else if(red < 0){
                    blue = 0;//最小取值0
                }

                imcolor.setBackgroundColor(Color.rgb(red,green,blue));

            }
        });

        TextWatcher tw = new MyWatcher(etgreen, imcolor); //new TextWatcher()接口不能new对象因为含有抽象方法
        etgreen.addTextChangedListener(tw);
    }
    class MyWatcher implements  TextWatcher
    {
        //MyWatcher 类需要访问另一个类ColorCombinActivity的成员（私有成员不能直接访问）
        //通过构造方法将需要访问的控件传送过来
        EditText etgreen;
        ImageView imcolor;
        public MyWatcher(EditText t, ImageView i)
        {
            etgreen = t;
            imcolor = i;
        }


        @Override
        public void beforeTextChanged(CharSequence s, int start, int count, int after) {
            //改变前要做的处理代码

        }

        @Override
        public void onTextChanged(CharSequence s, int start, int before, int count) {
            //改变时要做的处理代码

        }

        @Override
        public void afterTextChanged(Editable s) {
            //改变完之后要做的处理代码
            //文本框输入完之后，获取文本框的输入值
            String greenString = etgreen.getText().toString();
            if(greenString.equals(""))
            {
                green = 0;
            }
            else
            {
                green = Integer.parseInt(greenString);//将字符串转换成整型数
            }
            //判断数的范围<255
            if(green > 255)
            {
                green = 255;//最大取值255
            }else if(green < 0){
                green = 0;//最小取值0
            }

            imcolor.setBackgroundColor(Color.rgb(red,green,blue));

        }
    }
}
